package it.softphone.cmc.server.rest;

import it.softphone.cmc.server.service.SystemManagerService;
import it.softphone.cmc.server.service.TenantService;
import it.softphone.cmc.shared.entity.Account;
import it.softphone.commons.service.LogAware;
import it.softphone.commons.service.security.SecurityAccountService;
import it.softphone.commons.spring.ApplicationConfig;

import javax.ws.rs.core.Response;


/**
 * 
 * @author softphone
 *
 */
public class RESTServiceAware implements LogAware {

	public static final String NO_REDIRECT 			= "blank";
	public static final String REDIRECT_URI_PARAM	= "redirect_uri";


	/**
	 * 
	 * @return
	 */
	protected static Account getloggedAccount() {
			return security().getPrincipal(Account.class);
		
	}

	/**
	 * 
	 * @return
	 */
	public static final TenantService tenant() {

		final TenantService service = ApplicationConfig.getBean(TenantService.class);
		
		return service;
	}
	
	/**
	 * 
	 * @return
	 */
	public static final SystemManagerService system() {

		final SystemManagerService service = ApplicationConfig.getBean(SystemManagerService.class);
		
		return service;
	}
	
	/**
	 * 
	 * @return
	 */
	public static final SecurityAccountService security() {
		
		SecurityAccountService service = ApplicationConfig.getBean(SecurityAccountService.class);
		
		return service;
		
	}
	

	protected void checkRole( String role ) throws CMCRestException {
		if( !security().hasRole(role) ) {
			final String entity = String.format("user not has the required [%s] role!", role);
					
			throw new CMCRestException( Response.Status.FORBIDDEN, entity);		
		}
	}
	
	protected void checkPermission( String permission ) throws CMCRestException {
		if( !security().isPermitted(permission) ) {
			final String entity = String.format("user not has the required [%s] permission!", permission);
					
			throw new CMCRestException( Response.Status.FORBIDDEN, entity);		
		}
	}
	

}
